package com.spzx.product.message;

import com.rabbitmq.client.Channel;
import com.spzx.common.core.utils.StringUtils;
import com.spzx.common.rabbit.constant.MqConst;
import com.spzx.product.service.SkuStockService;
import lombok.SneakyThrows;
import org.springframework.amqp.core.Message;
import org.springframework.amqp.rabbit.annotation.Exchange;
import org.springframework.amqp.rabbit.annotation.Queue;
import org.springframework.amqp.rabbit.annotation.QueueBinding;
import org.springframework.amqp.rabbit.annotation.RabbitListener;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.scheduling.annotation.Async;
import org.springframework.stereotype.Component;

@Component
public class MinusStockAddSalesReceive {



    @Autowired
    private SkuStockService skuStockService;

    @RabbitListener(bindings = @QueueBinding(
            exchange = @Exchange(value = MqConst.EXCHANGE_PRODUCT),
            value = @Queue(value = MqConst.QUEUE_MINUS),
            key = MqConst.ROUTING_MINUS
    ))
    @SneakyThrows
    public void minusStockAddSales(String orderNo, Message message, Channel channel){

        //判断是否为空
        if (StringUtils.hasText(orderNo)){
            try {
                //更新订单状态
                skuStockService.minusStockAddSales(orderNo);
                //确认消息
                channel.basicAck(message.getMessageProperties().getDeliveryTag(),false);
            } catch (Exception e) {
                //throw new RuntimeException(e);
                //退回消息
                channel.basicNack(message.getMessageProperties().getDeliveryTag(),false,true);
            }
        }

    }
}
